package kusterordonio.interfaz;

import java.awt.event.KeyEvent;
import java.util.*;
import java.io.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.ImageIcon;
import kusterordonio.dominio.*;
import javax.swing.JOptionPane;

public class VentanaPrincipal extends javax.swing.JFrame {

    private SistemaCarne modelo;
    
    /** Creates new form VentanaPrincipal */
    public VentanaPrincipal(SistemaCarne unSistema) {
        modelo = unSistema;
        this.setLocation(500, 400);
        this.setBounds(500,400,500,400);
        initComponents();
        jBtnOk1.setMnemonic(KeyEvent.VK_O);
        jBtnOk2.setMnemonic(KeyEvent.VK_K);
        jBtnSalir.setMnemonic(KeyEvent.VK_S);
    }
    
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPLoginPMT = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jBtnOk2 = new javax.swing.JButton();
        JtxtPMT = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jPLoginMedico1 = new javax.swing.JPanel();
        jLabel3 = new javax.swing.JLabel();
        jBtnOk1 = new javax.swing.JButton();
        JtxtMedico = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jBtnSalir = new javax.swing.JButton();
        jLabel6 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Sistema de Carnés de Salud");
        setBounds(new java.awt.Rectangle(200, 100, 450, 390));
        setMinimumSize(new java.awt.Dimension(450, 390));
        setResizable(false);

        jPanel1.setLayout(null);

        jPLoginPMT.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPLoginPMT.setOpaque(false);
        jPLoginPMT.setLayout(null);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel1.setText("CI:");
        jPLoginPMT.add(jLabel1);
        jLabel1.setBounds(40, 40, 40, 20);

        jBtnOk2.setText("OK");
        jBtnOk2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnOk2ActionPerformed(evt);
            }
        });
        jPLoginPMT.add(jBtnOk2);
        jBtnOk2.setBounds(50, 80, 80, 22);
        jPLoginPMT.add(JtxtPMT);
        JtxtPMT.setBounds(80, 40, 70, 20);

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel2.setText("Login Madre/Padre/Tutor");
        jPLoginPMT.add(jLabel2);
        jLabel2.setBounds(12, 13, 150, 14);

        jPanel1.add(jPLoginPMT);
        jPLoginPMT.setBounds(210, 280, 180, 120);

        jPLoginMedico1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        jPLoginMedico1.setOpaque(false);
        jPLoginMedico1.setLayout(null);

        jLabel3.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel3.setText("CI:");
        jPLoginMedico1.add(jLabel3);
        jLabel3.setBounds(40, 40, 40, 20);

        jBtnOk1.setText("OK");
        jBtnOk1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnOk1ActionPerformed(evt);
            }
        });
        jPLoginMedico1.add(jBtnOk1);
        jBtnOk1.setBounds(50, 80, 80, 22);
        jPLoginMedico1.add(JtxtMedico);
        JtxtMedico.setBounds(80, 40, 70, 20);

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11));
        jLabel4.setText("Login  Medico");
        jPLoginMedico1.add(jLabel4);
        jLabel4.setBounds(51, 13, 89, 14);

        jPanel1.add(jPLoginMedico1);
        jPLoginMedico1.setBounds(210, 150, 180, 120);

        jBtnSalir.setText("Salir");
        jBtnSalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jBtnSalirActionPerformed(evt);
            }
        });
        jPanel1.add(jBtnSalir);
        jBtnSalir.setBounds(490, 480, 80, 23);

        jLabel6.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabel6.setIcon(new javax.swing.ImageIcon(getClass().getResource("/kusterordonio/recursos/Tapa.jpg"))); // NOI18N
        jLabel6.setVerticalAlignment(javax.swing.SwingConstants.TOP);
        jPanel1.add(jLabel6);
        jLabel6.setBounds(0, 0, 610, 520);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 610, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 518, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void jBtnOk2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnOk2ActionPerformed
        boolean existeMadre= modelo.existePersona(modelo.getListaMadres(), JtxtPMT.getText());
        if(existeMadre){
            Persona m =  modelo.devuelvePersona(JtxtPMT.getText());
            VentanaListaCarnes listaCarne = new VentanaListaCarnes(modelo, false, modelo.devuelvePersona(JtxtPMT.getText()));
            listaCarne.setVisible(true);
        }
        else{
            boolean existePadre= modelo.existePersona(modelo.getListaPadres(), JtxtPMT.getText());
            if (existePadre){
                VentanaListaCarnes listaCarne = new VentanaListaCarnes(modelo, false, modelo.devuelvePersona(JtxtPMT.getText()));
                listaCarne.setVisible(true);
            }
            else{
                boolean existeTutor = modelo.existePersona(modelo.getListaTutores(), JtxtPMT.getText());
                if (existeTutor){
                    VentanaListaCarnes listaCarne = new VentanaListaCarnes(modelo, false, modelo.devuelvePersona(JtxtPMT.getText()));
                listaCarne.setVisible(true);
                }
                else{
                    JOptionPane.showMessageDialog(null,"La persona no existe", "Error", 2);
                }
            }
        }
        JtxtPMT.setText("");
    }//GEN-LAST:event_jBtnOk2ActionPerformed

    private void jBtnOk1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnOk1ActionPerformed
       boolean existe= modelo.existePersona(modelo.getListaMedicos(), JtxtMedico.getText());
        if(!existe){
            JOptionPane.showMessageDialog(null,"El medico no existe", "Error", 2);
        }else{
            VentanaListaCarnes listaCarne = new VentanaListaCarnes(modelo, true, modelo.devuelvePersona(JtxtMedico.getText()));
            listaCarne.setVisible(true);
        }
        JtxtMedico.setText("");
    }//GEN-LAST:event_jBtnOk1ActionPerformed

    private void jBtnSalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBtnSalirActionPerformed
        modelo.serializar(modelo);
        this.dispose();
    }//GEN-LAST:event_jBtnSalirActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField JtxtMedico;
    private javax.swing.JTextField JtxtPMT;
    private javax.swing.JButton jBtnOk1;
    private javax.swing.JButton jBtnOk2;
    private javax.swing.JButton jBtnSalir;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JPanel jPLoginMedico1;
    private javax.swing.JPanel jPLoginPMT;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
